import { Component, OnInit, Input, SimpleChanges, OnChanges, OnDestroy } from '@angular/core';
import { ConvergenceBox } from '../convergencebox';
import { ModelService } from '../../../../service/model.service';
import { ModelParamService } from '../../../../service/model-param.service';
import { DataSourceService } from '../../../../service/data-source/data-source.service';
import { Model } from '../../../../entity/model.model';
import { ModelParam } from '../../../../entity/model-Param';
// import { TagData } from '../../../../service/data-source/tag-protocol';

import { List } from 'linqts';
import { Tag } from '../../../../entity/tag.model';
import { Http } from '@angular/http';
import { DatePipe } from '@angular/common';
import { TagData } from "../../../../entity/tag-data.model";
import { SetofvalueService } from "../../../../service/setofvalue.service";
import { Setofvalue } from "../../../../entity/setofvalue.model";
import { InterceptorService } from '../../../../service/interceptor/interceptor-service';

@Component({
  selector: 'app-convergencebox-detail',
  templateUrl: './convergencebox-detail.component.html',
  styleUrls: ['./convergencebox-detail.component.css']
})
export class ConvergenceboxDetailComponent implements OnInit, OnChanges, OnDestroy {
  /**
 * 双击的设备详细信息
 */
  @Input() convergenceBox: ConvergenceBox;

  public model: Model;
  /**
 * 所有型号
 */
  public models: Model[] = [];
  /**
 * 型号参数
 */
  public modelParams: ModelParam[] = [];
  /**
 * 展示的测点种类数据
 */
  public TagDatas:any[] = [];
  public swithTagDatas: TagData[] = [];
  public analogTagDatas: TagData[] = [];
  /**
  * 测点种类
  */
  public setofvalues: Setofvalue[] = [];
  private dataSource: DataSourceService = null;

  constructor(
    private http: InterceptorService,
    private datePipe: DatePipe,
    private modelService: ModelService,
    private modelParamService: ModelParamService,
    private setofvalueService: SetofvalueService, ) {
  }

  ngOnInit() {
  }
  ngOnChanges(changes: SimpleChanges): void {
    if (changes['convergenceBox'] != null && changes['convergenceBox'].currentValue != null) {
      this.loadConvergenceBoxInfo();
    }
  }
  ngOnDestroy(): void {
    if (this.dataSource != null) {
      this.dataSource.dispose();
    }
  }
  private loadConvergenceBoxInfo(): void {

    if (this.dataSource != null) {
      this.dataSource.dispose();
    }
    this.dataSource = new DataSourceService(this.http, this.datePipe);
    this.model = null;
    this.modelParams = [];
    this.swithTagDatas = [];
    this.analogTagDatas = [];
    this.TagDatas = [];
    // const modelGet = this.modelService.getModelByModelId(this.convergenceBox.modelId);
    const modelGet = this.modelService.getModelWithFactory();
    const tagGet = this.modelService.getTagByModelId(this.convergenceBox.modelId);
    const modelParamGet = this.modelParamService.getModelParamsByModeID(this.convergenceBox.modelId);
    const setofValueGet = this.setofvalueService.getSetofValuesBykeyID(510);//后添加的
    const dataSource = this.dataSource;
    Promise.all([modelGet, tagGet, modelParamGet, setofValueGet])
      .then(res => {

        if (res[0]) {
          this.models = [...res[0]];
        };
        let index = this.models.findIndex(o => o.modelId == this.convergenceBox.modelId);
        if (index >= 0) {
          this.model = this.models[index];
        } else {
          this.model = null;
        }

        if (res[2].length > 0) {
          this.modelParams = res[2];
        } else {
          this.modelParams = [];
        }

        if (res[3].length > 0) {
          this.setofvalues = [...res[3]];
          console.log(this.setofvalues)
        } else {
          this.setofvalues = [];
        }

        if (res[1] != null && res[1].length > 0) {
          const tags = new List(res[1]);
          const tagGroups = tags.GroupBy(group => group.tagKind, map => map);
          const tagKinds = Object.getOwnPropertyNames(tagGroups);
          // this.setofvalues.forEach(setofvalue => {
            
            tagKinds.forEach(tagKind => {
              let tagDataAry = [];
              if (tagKind != null) {
                const tagKindOfTags: Tag[] = Object.getOwnPropertyDescriptor(tagGroups, tagKind).value;

                tagKindOfTags.forEach(tag => {
                  const tagData: TagData = this.dataSource.binding([this.convergenceBox.farmCode, this.convergenceBox.deviceCode], tag);
                  tagData.tagUnit = tag.tagUnit ? "(" + tag.tagUnit + ")" : tag.tagUnit;   
                  tagDataAry.push(tagData)  
                });
                tagDataAry['name'] = this.setofvalues.filter(o =>o.valueCode == tagKind)[0].valueDisplay;
                this.TagDatas.push(tagDataAry)
              }
            });
            console.log(this.TagDatas);
          // })

          dataSource.begin();
        }
      })
      .catch(err => {
        console.log(err);
      });
    // Promise.all([modelGet, tagGet, modelParamGet,setofValueGet])
    //   .then(res => {
    //     if(res[0]){
    //       this.models = [...res[0]];
    //     };
    //     let index = this.models.findIndex(o => o.modelId == this.convergenceBox.modelId);
    //     if(index >= 0) {
    //       this.model = this.models[index];
    //     } else {
    //       this.model = null;
    //     }
    //     if (res[2].length > 0) {
    //       this.modelParams = res[2];
    //     } else {
    //       this.modelParams = [];
    //     }
    //     if (res[1] != null && res[1].length > 0) {
    //       const tags = new List(res[1]);
    //       const tagGroups = tags.GroupBy(group => group.tagKind, map => map);
    //       const tagKinds = Object.getOwnPropertyNames(tagGroups);
    //       tagKinds.forEach(tagKind => {
    //         if (tagKind != null) {
    //           const tagKindOfTags: Tag[] = Object.getOwnPropertyDescriptor(tagGroups, tagKind).value;
    //           console.log(tagKindOfTags)
    //           tagKindOfTags.forEach(tag => {

    //             const tagData: TagData = this.dataSource.binding([this.convergenceBox.farmCode, this.convergenceBox.deviceCode], tag);
    //             tagData.tagUnit = tag.tagUnit ? "(" + tag.tagUnit + ")" : tag.tagUnit;
    //             if (tagKind === '2') {
    //               this.swithTagDatas.push(tagData);
    //             } else {
    //               this.analogTagDatas.push(tagData);
    //             }
    //           });
    //         }
    //       });
    //       dataSource.begin();
    //     }
    //   })
    //   .catch(err => {
    //     console.log(err);
    //   });

  }
}



// WEBPACK FOOTER //
// ./src/app/module/monitor/convergencebox-list/convergencebox-detail/convergencebox-detail.component.ts